package util;

import java.io.File;
import java.io.RandomAccessFile;

import core.MusicLibrary;
import core.Song;

public class FileUtils {

	private static final String[] EXTENSIONS = new String[] { ".mp3", ".ogg" };

	private static final String[] GENRES = { "Blues", "Classic Rock",
			"Country", "Dance", "Disco", "Funk", "Grunge", "Hip-Hop", "Jazz",
			"Metal", "New Age", "Oldies", "Other", "Pop", "R&B", "Rap",
			"Reggae", "Rock", "Techno", "Industrial", "Alternative", "Ska",
			"Death Metal", "Pranks", "Soundtrack", "Euro-Techno", "Ambient",
			"Trip-Hop", "Vocal", "Jazz+Funk", "Fusion", "Trance", "Classical",
			"Instrumental", "Acid", "House", "Game", "Sound Clip", "Gospel",
			"Noise", "AlternRock", "Bass", "Soul", "Punk", "Space",
			"Meditative", "Instrumental Pop", "Instrumental Rock", "Ethnic",
			"Gothic", "Darkwave", "Techno-Industrial", "Electronic",
			"Pop-Folk", "Eurodance", "Dream", "Southern Rock", "Comedy",
			"Cult", "Gangsta", "Top 40", "Christian Rap", "Pop/Funk", "Jungle",
			"Native American", "Cabaret", "New Wave", "Psychedelic", "Rave",
			"Showtunes", "Trailer", "Lo-Fi", "Tribal", "Acid Punk",
			"Acid Jazz", "Polka", "Retro", "Musical", "Rock & Roll",
			"Hard Rock", "Folk", "Folk-Rock", "National Folk", "Swing",
			"Fast Fusion", "Bebob", "Latin", "Revival", "Celtic", "Bluegrass",
			"Avantgarde", "Gothic Rock", "Progressive Rock",
			"Psychedelic Rock", "Symphonic Rock", "Slow Rock", "Big Band",
			"Chorus", "Easy Listening", "Acoustic", "Humour", "Speech",
			"Chanson", "Opera", "Chamber Music", "Sonata", "Symphony",
			"Booty Brass", "Primus", "Porn Groove", "Satire", "Slow Jam",
			"Club", "Tango", "Samba", "Folklore", "Ballad", "Power Ballad",
			"Rhytmic Soul", "Freestyle", "Duet", "Punk Rock", "Drum Solo",
			"Acapella", "Euro-House", "Dance Hall", "unknown" };

	private static final String ISO = new String("ISO-8859-1");

	public static boolean isSound(File file) {
		String name = file.getName();
		for (String ext : EXTENSIONS)
			if (name.toLowerCase().endsWith(ext))
				return true;
		// System.out.println("dont know: " + file.getName());
		return false;
	}

	private static final byte[] tagBytes = new byte[128];

	public static Song readTag(File file, MusicLibrary requestor) {
		RandomAccessFile in;
		try {
			in = new RandomAccessFile(file, "r");
			in.seek(in.length() - 128);
			in.read(tagBytes);
			in.close();
		} catch (Exception e) {
			return null;
		}

		if (((char) tagBytes[0] == 'T') && ((char) tagBytes[1] == 'A')
				&& ((char) tagBytes[2] == 'G')) {
			Song song = new Song(requestor);
			if ((char) tagBytes[3] == '+') {
				song.setTitle("TAG+!!!!!!!!!!!");
				return song;
			} else {
				try {
					song.setTitle(new String(tagBytes, 3, 30, ISO).trim());
					song.setArtist(new String(tagBytes, 33, 30, ISO).trim());
					song.setAlbum(new String(tagBytes, 63, 30, ISO).trim());
					song.setYear(Short
							.valueOf(new String(tagBytes, 93, 4, ISO)));
					song.setComment(new String(tagBytes, 97, 28, ISO).trim());
					song.setTrack(tagBytes[126]);
					song.setGenre(GENRES[tagBytes[127]]);
					return song;
				} catch (Exception e) {
					return null;
				}
			}
		} else {
			return null;
		}
	}

}
